<template>
  <!-- 宠物管理组件 -->
  <div class="Pet">
    <!-- 面包屑路径导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>世界管理</el-breadcrumb-item>
      <el-breadcrumb-item>宠物管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容区 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <div class="grid-content search-container">
            <el-input
              placeholder="请输入内容"
              v-model.trim="searchCont"
              clearable
              class="search-input"
              @clear="searchClear"
            >
            </el-input>
            <el-button
              type="info"
              plain
              icon="el-icon-search"
              @click="getPetList"
            ></el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 宠物列表内容 -->
      <div class="pet-table">
        <el-table :data="petList" border style="width: 100%">
          <!-- <el-table-column type="index" label="#" width="48"></el-table-column> -->
          <el-table-column prop="pet_id" label="宠物ID" width="100" align="center"></el-table-column>
          <el-table-column label="宠物预览" align="center" width="100">
            <template slot-scope="props">
              <div v-if="props.row.pet_icon">
                <el-image :src="path + props.row.pet_icon">
                  <span slot="error">无宠物图标</span>
                </el-image>
              </div>
              <div v-else>
                无宠物图标
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="pet_name" label="宠物名称" align="center" width="150"> </el-table-column>
          <el-table-column prop="pet_desc" label="宠物描述"></el-table-column>
          <el-table-column prop="pet_descD" label="死亡信息"> </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="props">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editPet(props.row.id)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deletePet(props.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageConfig.currentPage"
          :page-sizes="pageConfig.pageSizes"
          :page-size="pageConfig.pageSize"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageConfig.pageTotal"
          :hide-on-single-page="false"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 修改宠物弹出层 -->
    <!-- <el-dialog
      title="编辑宠物"
      :visible.sync="dialogConfig.dialogEditPetVisible"
      class="edit-Pet-dialog"
    >
      <el-form
        :model="editPetForm"
        :rules="editPetFormRules"
        ref="editPetForm"
        class="demo-ruleForm"
        :label-position="'right'"
        label-width="80px"
      >
        <el-form-item label="宠物ID">
          <el-input v-model.trim="editPetForm.PetId" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属区域">
          <el-input v-model.trim="editPetForm.PetStreet" disabled></el-input>
        </el-form-item>
        <el-form-item label="宠物名称" prop="PetName">
          <el-input v-model.trim="editPetForm.PetName"></el-input>
        </el-form-item>
        <el-form-item label="宠物描述" prop="PetDesc">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="editPetForm.PetDesc"
            resize="none"
          >
          </el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button @click="dialogConfig.dialogEditPetVisible = false"
            >取消</el-button
          >
          <el-button type="primary" @click="editPetSubmit('editPetForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog> -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索内容
      searchCont: '',
      // 宠物列表数据
      petList: [],
      // 资源路径
      path: '',
      // 分页器参数配置
      pageConfig: {
        pageTotal: 0, // 分页器总条数
        pageSize: 10, // 每页显示条数
        pageSizes: [10, 15, 20, 25], // 可选择的每页条数
        currentPage: 1 // 分页器当前页码
      },
      // 加载层参数配置
      loadingConfig: {
        target: 'body',
        fullscreen: true,
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.1)',
        text: '拼命加载中'
      },
      // 弹出层配置
      dialogConfig: {
        dialogEditPetVisible: false
      },
      // 编辑宠物数据
      editPetForm: {
        PetId: '',
        PetStreet: '',
        PetName: '',
        PetDesc: '',
        PetBgm: ''
      },
      // 编辑宠物表单验证
      editPetFormRules: {
        petName: [
          { required: true, message: '请输入宠物名称', trigger: 'blur' },
          {
            min: 1,
            max: 12,
            message: '宠物名称在 1 到 12 个字符',
            trigger: 'blur'
          }
        ],
        petDesc: [
          {
            min: 0,
            max: 255,
            message: '宠物描述不超过255个字符',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    getPetList () {
      // 获取宠物列表
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      this.$axios
        .get('/petList', {
          params: {
            search: this.searchCont.trim(),
            pagenum: this.pageConfig.currentPage,
            pagesize: this.pageConfig.pageSize
          }
        })
        .then(res => {
          if (res.status === 0) {
            // 设置分页
            this.pageConfig.pageTotal = res.total;
            this.pageConfig.currentPage = res.pagenum;
            // 设置数据
            this.petList = res.data;
            this.path = res.path;
            // 判断当前页是否为空
            if (
              this.pageConfig.pageTotal ===
              (this.pageConfig.currentPage - 1) * this.pageConfig.pagesize
            ) {
              if (this.pageConfig.currentPage - 1 < 1) {
                this.handleCurrentChange(1);
              } else {
                this.handleCurrentChange(this.pageConfig.currentPage - 1);
              }
            }
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    searchClear () {
      this.getPetList();
    },
    editPet (id) {
      // 根据宠物ID查找信息
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      // 发起请求
      this.$axios
        .get('/petinfo/' + id)
        .then(res => {
          if (res.status === 0) {
            // 设置数据
            this.editPetForm.PetId = res.data.PetId;
            this.editPetForm.PetStreet = res.data.PetStreet;
            this.editPetForm.PetName = res.data.PetName;
            this.editPetForm.PetDesc = res.data.PetDesc;
            this.editPetForm.PetBgm = res.data.PetBgm;
            // 打开弹出层
            this.dialogConfig.dialogEditPetVisible = true;
          } else {
            // 提示失败信息
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    editPetSubmit (formName) {
      // 修改宠物提交
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 打开加载层
          var loading = this.$loading.service(this.loadingConfig);
          this.$axios
            .put('/editPet/' + this.editPetForm.PetId, {
              PetName: this.editPetForm.PetName,
              PetDesc: this.editPetForm.PetDesc
            })
            .then(res => {
              if (res.status === 0) {
                // 关闭弹出层
                this.dialogConfig.dialogEditPetVisible = false;
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getPetList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        }
      });
    },
    deletePet (id) {
      // 删除宠物
      // 调用确认框
      this.$confirm('你确定要删除该宠物吗？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      })
        .then(() => {
          // 打开加载层
          var loading = this.$loading.service(this.$store.state.loadingConfig);
          this.$axios
            .delete('/deletePet/' + id)
            .then(res => {
              if (res.status === 0) {
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getPetList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        })
        .catch(() => {});
    },
    handleSizeChange (val) {
      // 切换每页条数重新获取数据
      this.pageConfig.pageSize = val;
      this.getPetList();
    },
    handleCurrentChange (val) {
      // 切换页码时重新获取数据
      this.pageConfig.currentPage = val;
      this.getPetList();
    }
  },
  mounted () {
    this.getPetList();
  }
};
</script>

<style lang="less" scoped>
.play-music {
  display: block;
  margin-left: 10px;
  color: #666666;
  font-size: 24px;
  text-decoration: none;
}
</style>
